public class test02 {
    public List<List<Integer>> threeSum(int[] nums) {
        //要点1：顺序不重要
        //要点2：不能重复
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        int sum = 0;
        for(int i = 0;i<nums.length;i++){
            if(nums[i]>0){
                return res;
            }
            if(i>0 && nums[i]==nums[i-1]){
                continue;
            }
            int left = i+1,right = nums.length-1;
            while(left<right){
                sum = nums[i]+nums[left]+nums[right];
                if(sum>0){
                    right--;
                }else if(sum<0){
                    left++;
                }else{
                    res.add(Arrays.asList(nums[i],nums[left],nums[right]));
                    while(left<right && nums[left]==nums[left+1])left++;
                    while(left<right && nums[right]==nums[right-1])right--;
                    //注意left、right同时更新放在else里面
                    left++;
                    right--;
                }
            }
        }
        return res;
    }
}
